define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {

            
            // 初始化表格参数
            Table.api.init({
                extend: {
                    index_url: 'onlineclassroom/onlineclassroom/index' + location.search,
                    add_url: 'onlineclassroom/onlineclassroom/add',
                    edit_url: 'onlineclassroom/onlineclassroom/edit',
                    del_url: 'onlineclassroom/onlineclassroom/del',
                    multi_url: 'onlineclassroom/onlineclassroom/multi',
                    import_url: 'onlineclassroom/onlineclassroom/import',
                    table: 'online_classroom',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                queryParams: function (params) {
                    // 添加城市和学校筛选参数
                    var cityId = $('#city-select').val();
                    var schoolId = $('#school-select').val();
                    if (cityId) {
                        params.city_id = cityId;
                    }
                    if (schoolId) {
                        params.school_id = schoolId;
                    }
                    return params;
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        { field: 'city_id', title: '城市', sortable: true },
                        { field: 'school_id', title: '学校', sortable: true },
                        {field: 'user_id', title: __('User_id')},
                        {field: 'user.username', title: __('Username'), operate: 'LIKE'},
                        {field: 'course_id', title: __('Course_id')},
                        {field: 'course_name', title: __('Course_name'), operate: 'LIKE'},
                        {field: 'course_type', title: __('Course_type'), searchList: {"video":__("Course_type video"),"audio":__("Course_type audio"),"text":__("Course_type text"),"live":__("Course_type live")}, formatter: Table.api.formatter.normal},
                        {field: 'chapter_name', title: __('Chapter_name'), operate: 'LIKE'},
                        {field: 'learning_duration', title: __('Learning_duration')},
                        {field: 'learning_progress', title: __('Learning_progress'), operate:'BETWEEN'},
                        {field: 'is_completed', title: __('Is_completed'), searchList: {"0":__("Is_completed 0"),"1":__("Is_completed 1")}, formatter: Table.api.formatter.normal},
                        {field: 'start_time', title: __('Start_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'device_type', title: __('Device_type'), searchList: {"pc":__("Device_type pc"),"mobile":__("Device_type mobile"),"tablet":__("Device_type tablet")}, formatter: Table.api.formatter.normal},
                        {field: 'score', title: __('Score'), operate:'BETWEEN'},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"0":__("Status 0"),"1":__("Status 1")}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 筛选按钮事件
            $('#filter-btn').on('click', function() {
                table.bootstrapTable('refresh');
            });

            // 重置筛选按钮事件
            $('#reset-filter-btn').on('click', function() {
                $('#city-select').val('');
                $('#school-select').val('');
                table.bootstrapTable('refresh');
            });
            // 引入通用的城市学校过滤JS
            var script = document.createElement('script');
            script.src = '/assets/js/backend/common/index.js';
            document.head.appendChild(script);
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});